#! /bin/bash
#
# Interactive make menu script
#

ECHO="/bin/echo"
PRINTF="/usr/bin/printf"
MAKE="/usr/bin/make"
MAKE_LOG_FILE="make_log.txt"

# Tests whether *entire string* is numerical.
isdigit()
{
    [ $# -eq 1 ] || return 0

    case $1 in
    *[!0-9]*) return 1;;
    *) return 0;;
    esac
}

make_clean()
{
    ${MAKE} clean
}

#$1 BOARD
#$2 command 
#r  release 
#d  debug 
#g  get 
#s  splint
issue_make_cmd()
{
    valid_board=0
    bIdx=0

    # Match against available target boards
    while [[ ${bIdx} -lt ${BOARD_NUM} ]]; do
        #${ECHO} "issue_make_cmd, b=${TARGET_BOARDS[${bIdx}]} p1=$1 p2=$2"
        if [[ ${TARGET_BOARDS[${bIdx}]} == $1 ]]; then
            valid_board=1
            break
        fi
        let bIdx+=1
    done
    if [[ ${valid_board} -eq 0 ]]; then
        ${ECHO} "Invalid BOARD:$1"
        return 1
    fi

    case $2 in
    'd')
    ${MAKE} BOARD=$1 DEBUG=y P2P_USE_STR=$3 DDR_USE_STR=$4 OEM_KEY_STR=$5 2>&1 |tee ${MAKE_LOG_FILE}
    return
    ;;

    'r')
    ${ECHO} P2P_USE_STR=$3 DDR_USE_STR=$4 OEM_KEY_STR=$5
    ${MAKE} BOARD=$1 P2P_USE_STR=$3 DDR_USE_STR=$4 OEM_KEY_STR=$5 2>&1 |tee ${MAKE_LOG_FILE}
    return
    ;;

    's')
    ${MAKE} splint BOARD=$1 P2P_USE_STR=$3 DDR_USE_STR=$4 OEM_KEY_STR=$5 2>&1 |tee ${MAKE_LOG_FILE}
    return
    ;;

    'g')
    if [[ ${CC_FTP_FOLDER[${bIdx}]} == "" ]]; then
        ${ECHO} "Invalid operation, this board is not found on FTP sever"
        return
    else
        ${SCRIPTS_DIR}/DownloadLib.sh ${CC_FTP_FOLDER[${bIdx}]} lib
    fi
    return
    ;;

    'p')
    ${MAKE} BOARD=$1 pack 2>&1 |tee ${MAKE_LOG_FILE}
    return
    ;;
	
    'u')
    ${MAKE} BOARD=$1 unpack 2>&1 |tee ${MAKE_LOG_FILE}
    return
    ;;
	
    *)
    ${ECHO} "issue_make_cmd, invalid build type:$2"
    exit -1
    ;;
    esac
}

# Append new board to this array
TARGET_BOARDS=("HI3520D_SINGLE_NOR_16M" "HI3516C_SINGLE_NOR_16M" "HI3535_SINGLE_NAND_128M" \
    "HI3536_SINGLE_NAND_128M" "HI3531_SINGLE_NAND_128M" "HI3520D_V300_SINGLE_NAND_128M" \
    "HI3531A_SINGLE_NAND_128M" "HI3521A_SINGLE_NAND_128M")
CC_FTP_FOLDER=("HI3520D_SINGLE_NOR_16M" "HI3516C_SINGLE_NOR_16M" "HI3535_SINGLE_NAND_128M" \
    "HI3536_SINGLE_NAND_128M" "HI3531_SINGLE_NAND_128M" "HI3520D_V300_SINGLE_NAND_128M" \
    "HI3531A_SINGLE_NAND_128M" "HI3521A_SINGLE_NAND_128M")
BOARD_NUM=${#TARGET_BOARDS[*]}

DEBUG_RELEASE_ADDON=100
RELEASE_START_CHOICE=0
RELEASE_END_CHOICE=`expr ${BOARD_NUM} - 1`
DEBUG_START_CHOICE=`expr ${RELEASE_START_CHOICE} + ${DEBUG_RELEASE_ADDON}`
DEBUG_END_CHOICE=`expr ${RELEASE_END_CHOICE} + ${DEBUG_RELEASE_ADDON}`
DWN_START_CHOICE=`expr ${DEBUG_START_CHOICE} + ${DEBUG_RELEASE_ADDON}`
DWN_END_CHOICE=`expr ${DEBUG_END_CHOICE} + ${DEBUG_RELEASE_ADDON}`
LINT_START_CHOICE=`expr ${DWN_START_CHOICE} + ${DEBUG_RELEASE_ADDON}`
LINT_END_CHOICE=`expr ${DWN_END_CHOICE} + ${DEBUG_RELEASE_ADDON}`
PACK_START_CHOICE=`expr ${LINT_START_CHOICE} + ${DEBUG_RELEASE_ADDON}`
PACK_END_CHOICE=`expr ${LINT_END_CHOICE} + ${DEBUG_RELEASE_ADDON}`
UNPACK_START_CHOICE=`expr ${PACK_START_CHOICE} + ${DEBUG_RELEASE_ADDON}`
UNPACK_END_CHOICE=`expr ${PACK_END_CHOICE} + ${DEBUG_RELEASE_ADDON}`

print_help()
{
    idx=0
    debug_idx=0
    download_idx=0
    splint_idx=0

    while [[ ${idx} -lt ${BOARD_NUM} ]]; do
        let debug_idx=${idx}+${DEBUG_RELEASE_ADDON}
        let download_idx=${debug_idx}+${DEBUG_RELEASE_ADDON}
        let splint_idx=${download_idx}+${DEBUG_RELEASE_ADDON}
        let pack_idx=${splint_idx}+${DEBUG_RELEASE_ADDON}
        let unpack_idx=${pack_idx}+${DEBUG_RELEASE_ADDON}

        ${PRINTF} "%-3d :\tmake %-10s release\n" ${idx} ${TARGET_BOARDS[${idx}]}
        ${PRINTF} "%-3d :\tmake %-10s debug  \n" ${debug_idx} ${TARGET_BOARDS[${idx}]}
        if [[ ${CC_FTP_FOLDER[${idx}]} != "" ]]; then
            ${PRINTF} "%-3d :\tget  %-10s lib    \n" ${download_idx} ${CC_FTP_FOLDER[${idx}]}
        fi
        ${PRINTF} "%-3d :\tlint %-10s \n" ${splint_idx} ${TARGET_BOARDS[${idx}]}
        ${PRINTF} "%-3d :\tmake %-10s pack \n" ${pack_idx} ${TARGET_BOARDS[${idx}]}
        ${PRINTF} "%-3d :\tmake %-10s unpack \n" ${unpack_idx} ${TARGET_BOARDS[${idx}]}
        ${PRINTF} "--------------------------------------\n"

        let idx=${idx}+1
    done

    ${PRINTF} "%-3s :\tMake clean\n" "c"
    ${PRINTF} "%-3s :\tHelp!!\n" "h"
    ${PRINTF} "%-3s :\tQuit\n" "q"
}

# $1 input interactively or from CLI parameter
action_on_input()
{
    idx=$1
    debug="r"

    # Check enter only case
    if [[ ${idx} == "" ]]; then
        ${ECHO} "Please make a choice"
        return 255
    fi

    # Check non-digital case
    if ! isdigit ${idx}; then
        ${ECHO} "${idx} contains non-digital char"
        return 255
    fi

    # Convert debug choice to release choice
    if   [[ ${idx} -ge ${RELEASE_START_CHOICE} && ${idx} -le ${RELEASE_END_CHOICE} ]]; then
        debug="r"
    elif [[ ${idx} -ge ${DEBUG_START_CHOICE} && ${idx} -le ${DEBUG_END_CHOICE} ]]; then
        debug="d"
        idx=`expr ${idx} - ${DEBUG_START_CHOICE}`
    elif [[ ${idx} -ge ${DWN_START_CHOICE} && ${idx} -le ${DWN_END_CHOICE} ]]; then
        debug="g"
        idx=`expr ${idx} - ${DWN_START_CHOICE}`
    elif [[ ${idx} -ge ${LINT_START_CHOICE} && ${idx} -le ${LINT_END_CHOICE} ]]; then
        debug="s"
        idx=`expr ${idx} - ${LINT_START_CHOICE}`
    elif [[ ${idx} -ge ${PACK_START_CHOICE} && ${idx} -le ${PACK_END_CHOICE} ]]; then
        debug="p"
        idx=`expr ${idx} - ${PACK_START_CHOICE}`
    elif [[ ${idx} -ge ${UNPACK_START_CHOICE} && ${idx} -le ${UNPACK_END_CHOICE} ]]; then
        debug="u"
        idx=`expr ${idx} - ${UNPACK_START_CHOICE}`
    else
        ${ECHO} "${idx} is out of range"
        return 255
    fi

    #${ECHO} idx=${idx} debug=${debug} BAORD=${TARGET_BOARDS[${idx}]}
    issue_make_cmd ${TARGET_BOARDS[${idx}]} ${debug}
}

build_menu()
{
    print_help
    while [ 1 ]; do
        $ECHO -n "MainMenu:"
        read input

        case ${input} in
        "") # Filter enter key
        continue;
        ;;

        'q')
        break
        ;;

        'h')
        print_help
        continue
        ;;
        
        'c')
        make_clean
        continue
        ;;

        esac
        action_on_input ${input}
    done
}

P2P_USE_VAL=HBYDT
DDR_USE_VAL=n
OEM_KEY_VAL=n

if [[ -z $1 && -z $2 ]]; then
    build_menu
else
    if [[ -z $3 ]]; then
        ${ECHO} Parameter 3, 4 and 5 not set, Use default
    else
        P2P_USE_VAL=$3
        if [[ -z $4 ]]; then
            ${ECHO} Parameter 4 and 5 not set, Use default
        else
            DDR_USE_VAL=$4
            if [[ -z $5 ]]; then
                ${ECHO} Parameter 5 not set, Use default
            else
                OEM_KEY_VAL=$5
            fi
        fi
    fi
    ${ECHO} OEM_KEY_VAL=$OEM_KEY_VAL
    issue_make_cmd $1 $2 $P2P_USE_VAL $DDR_USE_VAL $OEM_KEY_VAL
fi

# vim:et:sw=4:ts=4

